/*
 * @Author: zxhuan
 * @Date: 2019-05-25 13:46:56
 * @blog: http//:www.zxhuan.com
 * @Last Modified time: 2019-05-25 13:46:56
 */
import { dataDecoder, dataEncoder } from '@/providers/Func'
import Toast from '../../static/vant/toast/toast'

class Ajax {
    // type=1默认loading，type=2手动loading
    static Post(url, parmas,type=1) {
        console.log(parmas)
        if(type===1){
            wx.showLoading({
                title:'加载中',
                mask:true
            })
        }
        return new Promise((resolve, reject) => {
            wx.request({
                url,
                method: 'POST',
                data: {
                    data: dataEncoder(parmas)
                },
                header: {
                    "content-type": "application/x-www-form-urlencoded"
                },
                success: (res) => { return Ajax.AjaxHandlerFunction(res.data,type, resolve, reject) },
                fail: (err) => { reject(err) }
            })
        })
    }
    static AjaxHandlerFunction(data,type,resolve, reject) {
        let _data = typeof data == 'string' ? JSON.parse(data) : data
        _data = dataDecoder(_data.Data)
        console.log(_data)
        switch (_data.Status) {
            case '1':
                resolve(_data.Data)
                if(type === 1){
                    wx.hideLoading()
                }
                break
            default:
                wx.hideLoading()
                Toast(_data.msg)
                reject(_data.msg)
                break;
        }
    }
}

export { Ajax }
